<template>
  <layout>
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索</a-button>
        <a-button
          class="action-button"
          type="primary"
          auth="project.add"
          @click="tableAdd"
        >新增</a-button>
      </custom-form>
      <!-- <a-input placeholder="产品名称或者编号" v-model="tableSearch.searchKey" allowClear style="width: 200px;" /> -->
    </section>

    <!-- 产品列表 -->
    <a-table
      :columns="tableColumns"
      :dataSource="tableData"
      :rowKey="record => record.id"
      :customRow="customTableRow"
      :pagination="false"
      bordered
    >
      <template
        slot="task_statu"
        slot-scope="row"
      >
        {{getLabel(row.task_statu, [], 'task_statu')}}
      </template>
      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-button
          class="action-button"
          type="primary"
          auth="project.edit"
          @click="handleTableEdit(row)"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            class="action-button"
            auth="project.delete"
            type="danger"
          >删除</a-button>
        </a-popconfirm>
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination>

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      @ok="handleSubmit"
      @cancel="visible = false"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        layout="horizontal"
      ></custom-form>
    </a-modal>

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { userServices, IProject } from '@/common/services/user'

import { statusType } from '@/common/enum'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  // 产品列表搜索条件
  public tableSearch = {
    store_id: '',
    page: 1,
    pageSize: 15,
    searchKey: ''
  }

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '项目编号',
      dataIndex: 'task_id',
      key: 'task_id',
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        disabled: true
      }
    },
    {
      title: '项目名称',
      dataIndex: 'task_nam',
      key: 'task_nam',
      form: {
        type: 'a-input',
        placeholder: '请输入项目名称',
        isShowQuery: true
      }
    },
    {
      title: '项目类型',
      dataIndex: 'task_usr_tab',
      key: 'task_usr_tab',
      form: {
        type: 'a-radio-group',
        placeholder: '请选择项目类型',
        dict: [
          {
            label: '一般农户',
            value: '一般农户'
          },
          {
            label: '种植大户',
            value: '种植大户'
          }
        ]
      }
    },
    {
      title: '项目使用地区编号',
      dataIndex: 'task_area_tab',
      key: 'task_area_tab',
      form: {
        type: 'a-input',
        placeholder: '请选择项目使用地区编号',
      }
    },
    {
      title: '项目状态',
      key: 'task_statu',
      form: {
        type: 'a-radio-group',
        placeholder: '请选择项目状态',
        dict: statusType
      },
      scopedSlots: { customRender: 'task_statu' }
    },
    {
      title: '项目依据文件名称',
      dataIndex: 'task_doc',
      key: 'task_doc',
      form: {
        type: 'a-textarea',
        placeholder: '请项输入目依据文件名称'
      }
    },
    {
      title: '操作',
      key: 'action',
      width: 300,
      scopedSlots: { customRender: 'action' }
    }
  ]

  public tableApi = {
    list: userServices.getProjectsList,
    add: userServices.updateProject,
    edit: userServices.updateProject,
    // del: poserServices.deletePowerGroup,
    // imageUpload: 'uploadStoreImage',
    // imageRemove: 'delStoreImage'
  }

  public mounted() {
    this.getData()
  }

  public async getData() {
    const res = await this.getTableList()
    this.tableData = res.data || []
  }

  // 更新当前数据状态
  public handleUpdateStatus(val: number, row: any) {
    const status = val ? '1' : '2'
    this.$api
      .updateStore({
        sid: row.sid,
        gid: row.gid,
        zt: status
      })
      .then((res: Api.ResponseOptions) => {
        row.zt = status
        this.$message.success('更新成功')
      })
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: any) {
    this.tableEdit(Object.assign({}, row))
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
